package com.fr.web.core;

import [Z;
import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.base.Style;
import com.fr.base.core.html.Tag;
import com.fr.base.core.json.JSONException;
import com.fr.report.CellElement;
import com.fr.report.DefaultCellElement;
import com.fr.report.ReportGetter;
import com.fr.report.cellElement.core.CellElementAttribute;
import com.fr.web.Browser;
import com.fr.web.Repository;
import com.fr.web.core.chwriter.CellHtmlWriter;
import com.fr.web.core.chwriter.PageCellWriter;
import com.fr.web.core.chwriter.WriteCellWriter;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HTMLWriter
{
  private static HTMLWriter SC = new HTMLWriter();
  static int ID = 0;
  private static int ID_MAX = 2147482647;

  public static HTMLWriter getInstance()
  {
    return SC;
  }

  protected static int _GENERATE_ID()
  {
    if (ID >= ID_MAX)
      ID = 0;
    return (++ID);
  }

  public void writeFrozenContentWithCenterCornerNW(Tag paramTag1, ReportGetter paramReportGetter, ColumnRow paramColumnRow, int paramInt, CellHtmlWriter paramCellHtmlWriter, Repository paramRepository, Tag paramTag2, Tag paramTag3, Tag paramTag4, Tag paramTag5)
  {
    int i = _GENERATE_ID();
    paramCellHtmlWriter.setTableID(i);
    paramTag1.sub(paramTag2.css("position", "absolute").css("overflow", "scroll").cls("offset-c frozen-center").sub(clipReport2Html(paramReportGetter, new Rectangle(paramColumnRow.getColumn(), paramColumnRow.getRow(), paramReportGetter.getColumnCount() - paramColumnRow.getColumn(), paramReportGetter.getRowCount() - paramColumnRow.getRow()), paramInt, paramCellHtmlWriter, paramRepository)));
    Tag localTag1 = paramTag3.css("position", "absolute").css("overflow", "hiden").cls("frozen-row frozen-column offset-c frozen-corner");
    if ((paramColumnRow.column != 0) && (paramColumnRow.row != 0))
      paramTag1.sub(localTag1.sub(clipReport2Html(paramReportGetter, new Rectangle(0, 0, paramColumnRow.getColumn(), paramColumnRow.getRow()), paramInt, paramCellHtmlWriter, paramRepository)));
    Tag localTag2 = paramTag4.css("position", "absolute").css("overflow-y", "scroll").cls("frozen-row offset-c frozen-north");
    if (paramColumnRow.row != 0)
      paramTag1.sub(localTag2.sub(clipReport2Html(paramReportGetter, new Rectangle(paramColumnRow.getColumn(), 0, paramReportGetter.getColumnCount() - paramColumnRow.getColumn(), paramColumnRow.getRow()), paramInt, paramCellHtmlWriter, paramRepository)));
    Tag localTag3 = paramTag5.css("position", "absolute").css("overflow-x", "scroll").cls("frozen-column offset-c frozen-west");
    if (paramColumnRow.column != 0)
      paramTag1.sub(localTag3.sub(clipReport2Html(paramReportGetter, new Rectangle(0, paramColumnRow.getRow(), paramColumnRow.getColumn(), paramReportGetter.getRowCount() - paramColumnRow.getRow()), paramInt, paramCellHtmlWriter, paramRepository)));
  }

  public void writeFrozenContent(Tag paramTag, ReportGetter paramReportGetter, ColumnRow paramColumnRow, int paramInt, CellHtmlWriter paramCellHtmlWriter, Repository paramRepository)
  {
    writeFrozenContentWithCenterCornerNW(paramTag, paramReportGetter, paramColumnRow, paramInt, paramCellHtmlWriter, paramRepository, new Tag("div"), new Tag("div"), new Tag("div"), new Tag("div"));
  }

  protected Tag clipReport2Html(ReportGetter paramReportGetter, Rectangle paramRectangle, int paramInt, CellHtmlWriter paramCellHtmlWriter, Repository paramRepository)
  {
    label217: Tag localTag6;
    Iterator localIterator2;
    Tag localTag1 = new Tag("table").cls("x-table").attr("id", "" + paramCellHtmlWriter.getTableID()).attr("cellSpacing", "0").attr("cellPadding", "0");
    if (paramCellHtmlWriter instanceof PageCellWriter)
      localTag1.css("position", "absolute").css("left", "0px");
    Tag localTag2 = new Tag("colgroup");
    localTag1.sub(localTag2);
    int i = 0;
    int j = paramRectangle.x;
    int k = paramRectangle.width + j;
    while (j < k)
    {
      int l = paramReportGetter.getColumnWidth(j);
      if (l == 0)
        break label217:
      localTag2.sub(new Tag("col").attr("col", j + "").css("width", l + "px"));
      i += l;
      ++j;
    }
    if (paramRepository.getBrowser().shouldDoSomeStuff4MissingRightBorderOfTable());
    localTag1.css("width", (i + 1) + "px");
    j = 0;
    Tag localTag3 = new Tag("tbody");
    localTag1.sub(localTag3);
    Tag localTag4 = null;
    [Z[] arrayOf[Z = new boolean[paramRectangle.height][];
    for (int i1 = 0; i1 < arrayOf[Z.length; ++i1)
    {
      arrayOf[Z[i1] = new boolean[paramRectangle.width];
      for (int i2 = 0; i2 < arrayOf[Z[i1].length; ++i2)
        arrayOf[Z[i1][i2] = 0;
    }
    Tag localTag5 = null;
    ArrayList[] arrayOfArrayList = new ArrayList[paramReportGetter.getRowCount()];
    int i3 = 0;
    CellElement localCellElement1 = null;
    CellElement localCellElement2 = null;
    int i4 = 0;
    int i5 = paramRectangle.y;
    int i6 = i5 + paramRectangle.height;
    while (i5 < i6)
    {
      ArrayList localArrayList;
      int i8;
      i4 = paramReportGetter.getRowHeight(i5);
      if (i5 == paramRectangle.y)
      {
        localTag4 = new Tag("tr").attr("id", "r-" + i5 + "-" + paramInt).attr("tridx", "" + i5);
        localTag3.sub(localTag4.css("height", i4 + "px"));
        dealWithTrTag(localTag4, i5 - paramRectangle.y);
        if (i4 == 0)
          paramCellHtmlWriter.dealWithEmptyHeight(localTag4);
      }
      j += i4;
      CellElement[] arrayOfCellElement = new CellElement[paramReportGetter.getColumnCount()];
      localIterator2 = paramReportGetter.getRow(i5);
      while (localIterator2.hasNext())
      {
        localCellElement1 = (CellElement)localIterator2.next();
        arrayOfCellElement[localCellElement1.getColumn()] = localCellElement1;
      }
      if (arrayOfArrayList[i5] != null)
      {
        localArrayList = arrayOfArrayList[i5];
        for (i8 = 0; i8 < localArrayList.size(); ++i8)
        {
          localCellElement1 = (CellElement)localArrayList.get(i8);
          arrayOfCellElement[localCellElement1.getColumn()] = localCellElement1;
        }
      }
      for (int i7 = 0; i7 < arrayOfCellElement.length; ++i7)
      {
        if (arrayOfCellElement[i7] == null)
          break label1678:
        localCellElement1 = arrayOfCellElement[i7];
        Rectangle localRectangle1 = paramRectangle.intersection(new Rectangle(localCellElement1.getColumn(), localCellElement1.getRow(), localCellElement1.getColumnSpan(), localCellElement1.getRowSpan()));
        if (localRectangle1.width > 0)
        {
          int i14;
          if (localRectangle1.height <= 0)
            break label1678:
          localCellElement2 = localCellElement1;
          int i9 = localRectangle1.y - paramRectangle.y;
          int i10 = localRectangle1.x - paramRectangle.x;
          if (i3 < i9)
          {
            for (int i11 = i3; i11 < i9; ++i11)
            {
              fillBlankTD(localTag4, arrayOf[Z, i11, paramRectangle.width, paramInt, paramCellHtmlWriter.getTableID(), paramRectangle, paramReportGetter, paramRepository);
              i4 = paramReportGetter.getRowHeight(i11 + paramRectangle.y + 1);
              localTag4 = new Tag("tr").attr("id", "r-" + (i11 + paramRectangle.y + 1) + "-" + paramInt).attr("tridx", "" + (i11 + paramRectangle.y + 1));
              localTag3.sub(localTag4.css("height", i4 + "px"));
              dealWithTrTag(localTag4, i11 + 1);
              if (i4 == 0)
                paramCellHtmlWriter.dealWithEmptyHeight(localTag4);
            }
            i3 = i9;
          }
          fillBlankTD(localTag4, arrayOf[Z, i9, i10, paramInt, paramCellHtmlWriter.getTableID(), paramRectangle, paramReportGetter, paramRepository);
          fillFrozenContentTD(arrayOf[Z, i9, i10, localRectangle1.height, localRectangle1.width);
          Rectangle localRectangle2 = new Rectangle(localRectangle1.x, localRectangle1.y, localRectangle1.width, localRectangle1.height);
          if ((localRectangle2.height > 1) || (localRectangle2.width > 1))
          {
            int i12;
            int i13;
            if ((paramReportGetter.getRowHeight(i5) == 0) || (paramReportGetter.getColumnWidth(localCellElement1.getColumn()) == 0))
            {
              int i16;
              i12 = i5;
              i13 = localCellElement1.getColumn();
              i14 = localRectangle2.height;
              int i15 = localRectangle2.width;
              if (localRectangle2.height > 1)
                for (i16 = 0; i16 < localRectangle2.height; ++i16)
                {
                  i12 = i5 + i16;
                  if (paramReportGetter.getRowHeight(i12) > 0)
                    break;
                  --i14;
                }
              if ((i14 > 0) && (localRectangle2.width > 1))
                for (i16 = 0; i16 < localRectangle2.width; ++i16)
                {
                  i13 = localCellElement1.getColumn() + i16;
                  if (paramReportGetter.getColumnWidth(i13) > 0)
                    break;
                  --i15;
                }
              if ((i14 > 0) && (i15 > 0))
              {
                Object localObject = null;
                try
                {
                  localObject = localCellElement1.clone(i13, i12, i15, i14);
                }
                catch (CloneNotSupportedException localCloneNotSupportedException)
                {
                  localObject = new DefaultCellElement(i13, i12, i15, i14, localCellElement1.getValue());
                  FRContext.getLogger().log(Level.WARNING, localCloneNotSupportedException.getMessage(), localCloneNotSupportedException);
                }
                ((CellElement)localObject).setAttribute(CellElementAttribute.HTML_ID, WriteCellWriter.retriveCellElementPositions(localCellElement1));
                localCellElement1.setAttribute(CellElementAttribute.HTML_ID, new Object());
                localRectangle2.height = 1;
                localRectangle2.width = 1;
                localRectangle1.height = 1;
                localRectangle1.width = 1;
                if (i12 == i5)
                {
                  arrayOfCellElement[i13] = localObject;
                }
                else
                {
                  if (arrayOfArrayList[i12] == null)
                    arrayOfArrayList[i12] = new ArrayList();
                  arrayOfArrayList[i12].add(localObject);
                }
              }
            }
            else
            {
              i12 = localRectangle2.height;
              i13 = localRectangle2.width;
              if (paramRepository.getBrowser().justDealWithTheHiddenRowsOfTail())
              {
                i14 = i12 - 1;
                while (true)
                {
                  if (i14 <= 0)
                    break label1548;
                  if (paramReportGetter.getRowHeight(i5 + i14) > 0)
                    break label1548:
                  localRectangle2.height -= 1;
                  --i14;
                }
              }
              for (i14 = 1; i14 < i12; ++i14)
                if (paramReportGetter.getRowHeight(i5 + i14) <= 0)
                  localRectangle2.height -= 1;
              for (i14 = 1; i14 < i13; ++i14)
                if (paramReportGetter.getColumnWidth(localCellElement1.getColumn() + i14) <= 0)
                  localRectangle2.width -= 1;
            }
          }
          try
          {
            localTag5 = paramCellHtmlWriter.cell2Tag(localCellElement1, paramReportGetter, localRectangle2, localRectangle1);
            localTag4.sub(localTag5);
            dealWithTdTag(localTag5, localCellElement1.getColumn() - paramRectangle.x, localCellElement1.getRow() - paramRectangle.y);
            localCellElement1.removeAttribute(CellElementAttribute.HTML_ID);
          }
          catch (JSONException localJSONException)
          {
            FRContext.getLogger().log(Level.WARNING, localJSONException.getMessage(), localJSONException);
          }
        }
      }
      label1548: label1678: ++i5;
    }
    if (localCellElement2 != null)
    {
      fillBlankTD(localTag4, arrayOf[Z, localCellElement2.getRow() - paramRectangle.y, paramRectangle.width, paramInt, paramCellHtmlWriter.getTableID(), paramRectangle, paramReportGetter, paramRepository);
      i5 = localCellElement2.getRowSpan();
      for (Iterator localIterator1 = 1; localIterator1 < i5; ++localIterator1)
      {
        localTag6 = fillBlankTR(paramReportGetter.getRowHeight(localIterator1 + localCellElement2.getRow()));
        localTag6.attr("tridx", "" + (localIterator1 + localCellElement2.getRow()));
        localTag3.sub(localTag6);
        dealWithTrTag(localTag6, localIterator1 + localCellElement2.getRow() - paramRectangle.y);
      }
      localIterator1 = paramRectangle.height - localCellElement2.getRow() - localCellElement2.getRowSpan();
      for (localIterator2 = localIterator1; localIterator2 > 0; --localIterator2)
      {
        localTag6 = fillBlankTR(paramReportGetter.getRowHeight(paramRectangle.height - 1 - localIterator2));
        localTag6.attr("tridx", "" + (paramRectangle.height - 1 - localIterator2));
        localTag3.sub(localTag6);
        dealWithTrTag(localTag6, paramRectangle.height - 1 - localIterator2 - paramRectangle.y);
      }
    }
    return ((Tag)localTag1);
  }

  protected void dealWithTrTag(Tag paramTag, int paramInt)
  {
  }

  protected void dealWithTdTag(Tag paramTag, int paramInt1, int paramInt2)
  {
  }

  public Tag writeJSReport(ReportGetter paramReportGetter, int paramInt, CellHtmlWriter paramCellHtmlWriter, Repository paramRepository)
  {
    int i = _GENERATE_ID();
    paramCellHtmlWriter.setTableID(i);
    return clipReport2Html(paramReportGetter, new Rectangle(0, 0, paramReportGetter.getColumnCount(), paramReportGetter.getRowCount()), paramInt, paramCellHtmlWriter, paramRepository);
  }

  protected void fillBlankTD(Tag paramTag, boolean[][] paramArrayOfBoolean, int paramInt1, int paramInt2, int paramInt3, int paramInt4, Rectangle paramRectangle, ReportGetter paramReportGetter, Repository paramRepository)
  {
    if ((paramInt2 < 0) || (paramInt1 < 0))
      return;
    for (int i = 0; i < paramInt2; ++i)
      if (paramArrayOfBoolean[paramInt1][i] == 0)
      {
        paramArrayOfBoolean[paramInt1][i] = 1;
        Tag localTag = new Tag("td");
        paramTag.sub(localTag);
        localTag.attr("id", ColumnRow.valueOf(paramRectangle.x + i, paramInt1 + paramRectangle.y) + "-" + paramInt3 + "-" + paramInt4).attr("row", "" + (paramInt1 + paramRectangle.y)).attr("col", "" + (paramRectangle.x + i));
        dealWithTdTag(localTag, i, paramInt1 - paramRectangle.y);
        HTMLWriterUtils.contentStyle2Css(Style.DEFAULT_STYLE, localTag);
        HTMLWriterUtils.borderStyle2Css(Style.DEFAULT_STYLE, localTag);
        if (paramReportGetter.getColumnWidth(paramRectangle.x + i) == 0)
          localTag.css("display", "none");
      }
  }

  protected void fillFrozenContentTD(boolean[][] paramArrayOfBoolean, int paramInt1, int paramInt2, int paramInt3, int paramInt4)
  {
    for (int i = 0; i < paramInt3; ++i)
      for (int j = 0; j < paramInt4; ++j)
        paramArrayOfBoolean[(i + paramInt1)][(j + paramInt2)] = 1;
  }

  protected Tag fillBlankTR(double paramDouble)
  {
    Tag localTag = new Tag("tr");
    if (paramDouble == 0.0D)
      localTag.css("display", "none");
    else
      localTag.css("height", paramDouble + "px");
    return localTag;
  }
}